<template>
    <div @click="onLogin1">
        登录 北纬
    </div>
    <div @click="onLogin2">
        登录 及购
    </div>
</template>

<script setup lang="ts">
    import { ref, onMounted } from "vue";
    import { useRoute, useRouter, LocationQuery } from "vue-router";
    import { ElMessage } from "element-plus";
    import { postApiLogin, getApiUserInfo, postApiFakeLogin } from "@/assets/api";
    import { _userStore, _mallStore } from "@/store/config";
    import { TypeLogin } from "@/interface/login";

    const route = useRoute();
    const router = useRouter();

    // 模拟登录
    interface Simulate extends LocationQuery {
        timestamp: string
        source: string
        signature: string
    }

    // 登录信息
    export interface Login {
        email: string
        password: string
        system: number
    }

    onMounted(() => {
        const _routeQuery: Simulate = route.query as Simulate;
        if (_routeQuery && _routeQuery.signature) {
            // 模拟登录
            onLogin(_routeQuery as Simulate);
        }
    })
    const params = ref<Login>({
        email: '',
        // password: '0c0634691f0e3baa',
        password: '123456',
        system: 5
    });
    // 北纬
    const onLogin1 = () => {
        params.value.email = 'beiwei@zhimatech.com';
        onLogin();
    }
    // 及购
    const onLogin2 = () => {
        params.value.email = 'jigou@zhimatech.com';
        onLogin();
    }
    // 账号登陆
    const onLogin = (query?: Simulate) => {
        ElMessage.closeAll();
        ElMessage({
            showClose: true,
            message: '登陆中...',
            type: 'success',
            duration: 0,
        });
        if (query && query.signature) {
            postApiFakeLogin(query).then((resp: TypeLogin.Response) => {
                handlerLogin(resp);
            }).catch((error) => {
                ElMessage.closeAll();
                ElMessage.error(error.response.data?.error_msg[0])
            })
        } else {
            postApiLogin(params.value, { noRepeat: 1 }).then((resp: TypeLogin.Response) => {
                handlerLogin(resp);
            })
        }
    }

    // 用户信息
    export interface ApiUserInfo {
        code: number
        data: {
            company_name: string
            username: string
        }
        msg: string
    }

    const handlerLogin = (resp: TypeLogin.Response) => {
        if (resp.access_token) {
            _userStore.$patch({
                token: resp.access_token,
                userId: +resp.user_id,
                isSuperManage: +resp.role_id === 1,
            });
            _mallStore.$patch({
                companyId: +resp.company_id,
                companyManager: +resp.company_manager,
                uniqueMember: +resp.unique_member,
            });
            getApiUserInfo().then((user: ApiUserInfo) => {
                ElMessage.closeAll();
                if (user.code === 200) {
                    ElMessage.success({
                        message: '登陆成功',
                        duration: 1000,
                    });
                    let { data } = user;
                    _mallStore.$patch({
                        companyName: data.company_name,
                        userName: data.username,
                    });
                    _mallStore.setCompanyName();
                    router.replace('/chooseMall');
                } else {
                    ElMessage.error(user.msg)
                }
            })
        } else {
            ElMessage.closeAll();
            ElMessage.error(resp.msg);
        }
    }
</script>

<style scoped lang="scss"></style>